package br.usp.ime.mac.mico.factories.tests;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import br.usp.ime.mac.mico.factories.JPADAOFactory;
import junit.framework.TestCase;

public class JPADAOFactoryTest extends TestCase {

	//Instance of JPADAOFactory to be tested.
	private JPADAOFactory jPADAOFactory = JPADAOFactory.getInstance();

	//This method closes the factory before ending the test run.
	protected void finalize() throws Throwable{
		jPADAOFactory.closeConnection();
	}

	public void testOpenConnection() {
		EntityManagerFactory eMFactory = jPADAOFactory.getFactory();
		EntityManager entityManager = eMFactory.createEntityManager();
		assertNotNull(entityManager);
	}

	public void testCloseConnection() {
		EntityManagerFactory eMFactory = jPADAOFactory.getFactory();

		assertTrue(eMFactory.isOpen());

		jPADAOFactory.closeConnection();
		assertFalse(eMFactory.isOpen());

		eMFactory = jPADAOFactory.openConnection();
		assertTrue(eMFactory.isOpen());
	}
}
